package com.hs.ding.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hs.ding.pojo.Dispatch;
import com.hs.ding.pojo.Transport;
import com.hs.ding.pojo.Vehicle;
import com.hs.ding.service.IDispatchService;
import com.hs.ding.service.ITransportService;
import com.hs.ding.utils.NumberGeneratorUtils;
import com.hs.dto.ResultBean;
import com.hs.li.service.ICarManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-06-18
 */
@CrossOrigin
@RestController
@RequestMapping("/transport")
public class TransportController {

    @Autowired
    private ITransportService iTransportService;

    @Autowired
    private IDispatchService iDispatchService;

    @Autowired
    private ICarManagementService carManagementService;

    @GetMapping("page")
    public ResultBean findAllByPage(Integer currentPage, Integer pageSize,String tOrderId){
        System.out.println("tOrderId = " + tOrderId);
        ResultBean allTransport = iTransportService.findAllTransport(currentPage, pageSize,tOrderId);
        return allTransport;
    }

    @PostMapping("/create")
    public ResultBean ceateNew(@RequestBody Transport transport){

        System.out.println("transport = " + transport);
        String id = transport.gettOrderId();
        UpdateWrapper<Transport> uw = new UpdateWrapper<>();
        uw.set("t_status",1);
        uw.eq("t_order_id",id);
        boolean b = iTransportService.update(uw);

        Dispatch dispatch = new Dispatch();
        dispatch.settOrderId(transport.gettOrderId());
        dispatch.setdOrderId(NumberGeneratorUtils.randomNumber());
        dispatch.setdStatus(0);
        dispatch.seteStatus(0);
        dispatch.setTransport(transport);

        boolean b1 = iDispatchService.save(dispatch);
        return b && b1 ? ResultBean.OK():ResultBean.ERROR();

    }

    @PostMapping("/delete/{tOrderId}")
    public ResultBean deleteTransport(@PathVariable String tOrderId){

        System.out.println("tOrderId = " + tOrderId);

        carManagementService.changeCarTypeByTransportId(tOrderId);

        return iTransportService.deleteTransport(tOrderId);

    }



}
